package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.*;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.*;
import com.iot.inter.entity.newpojo.response.subResponse.*;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;

/**
 * 操作套餐订单：
 * 1，取消流量订单
 * 2，暂停流量订单
 * 3，重启流量订单
 * 4，提前结束订单
 * 5，开通流量订单
 * 6，续订流量订单
 *
 * @author lm
 * @date 2019/8/5
 */

public class TrunToOperPackageOrderService {

    private static Logger logger = Logger.getLogger(TrunToOperPackageOrderService.class);

    /**
     * 去北京做：3.20取消套餐订单:2.0接口
     * <p>
     * 接入方可以通过调用该接口，取消之前订购的订单。
     * 取消的前提条件是，订单还未启用或者未支付;
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager cancelOrder(String orderId, String secret, String partnerCode) {

        try {

            CancelOrderRequestBody body = new CancelOrderRequestBody();
            body.setOrderId(orderId);

            // 请求参数拼接
            CancelOrderRequest request = new CancelOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_cancelOrderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(CancelOrderRequest.class).toXml(request, "utf-8");
            logger.info("【取消套餐订单】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_cancelOrderPackage);

            logger.info("【取消套餐订单】xml响应：" + result);

            CancelOrderResponse responseObj = new JaxbUtil(CancelOrderResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (responseObj != null) {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode("9998");
                user.setResultInfo("响应为空");
                return user;
            }

        } catch (Exception e) {
            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }

    }

    /**
     * 去北京做：3.8	暂停流量套餐订单:2.0接口
     * <p>
     * 接入方可以通过调用该接口，对某个设备进行停机操作，关闭其数据流量服务。
     * 停机后的设备，如果在指定的周期内没有复机，那么此设备将被废弃，占用的码号数据将被回收。
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager suspendOrder(String deviceId, String orderId, String secret, String partnerCode) {
        try {

            SuspendOrderRequestBody body = new SuspendOrderRequestBody();
            body.setOrderId(orderId);
            body.setDeviceId(deviceId);

            // 请求参数拼接
            SuspendOrderRequest request = new SuspendOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_suspendOrderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SuspendOrderRequest.class).toXml(request, "utf-8");
            logger.info("【暂停流量套餐订单】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_suspendOrderPackage);

            logger.info("【暂停流量套餐订单】xml响应：" + result);

            SuspendOrderResponse responseObj = new JaxbUtil(SuspendOrderResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (responseObj != null && StringUtils.isNotBlank(retCode)) {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode("9999");
                user.setResultInfo("出现了异常！");
                return user;
            }

        } catch (Exception e) {

            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }

    }

    /**
     * 去北京做：3.9	重新启动流量套餐订单:2.0接口
     * <p>
     * 接入方可以通过调用该接口，对某个已停机的设备重新恢复其数据流量服务。
     * 但是必须在IOT系统指定的停机保服务周期之内。
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager restarOrder(String deviceId, String orderId, String secret, String partnerCode) {

        try {
            RestarOrderRequestBody body = new RestarOrderRequestBody();
            body.setOrderId(orderId);
            body.setDeviceId(deviceId);

            // 请求参数拼接
            RestarOrderRequest request = new RestarOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_restarOrderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SuspendOrderRequest.class).toXml(request, "utf-8");
            logger.info("【重新启动流量套餐订单】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_restarOrderPackage);

            logger.info("【重新启动流量套餐订单】xml响应：" + result);

            RestarOrderResponse responseObj = new JaxbUtil(RestarOrderResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (StringUtils.isNotBlank(retCode)) {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode("9999");
                user.setResultInfo("出现了异常！");
                return user;
            }

        } catch (Exception e) {

            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }

    }

    /**
     * 去北京做 3.22	提前结束套餐订单:2.0接口
     *
     * @author lm
     * @date 2019/3/4
     */
    public OrderPaymentManager aheadFinish(String orderId, String deviceType, String aheadFinishCause, String secret, String partnerCode) {
        try {
            //请求参数处理
            AheadFinishDeviceOrderRequestBody body = new AheadFinishDeviceOrderRequestBody();
            body.setOrderCode(orderId);
            body.setDeviceType(deviceType);
            body.setAheadFinishCause(aheadFinishCause);
            // 请求参数拼接
            AheadFinishDeviceOrderRequest request = new AheadFinishDeviceOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_aheadFinishDeviceOrder, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(AheadFinishDeviceOrderRequest.class).toXml(request, "utf-8");
            logger.info("【提前结束套餐订单】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_aheadFinishDeviceOrder);

            logger.info("【提前结束套餐订单】xml响应：" + result);
            AheadFinishDeviceOrderResponse responseObj = new JaxbUtil(AheadFinishDeviceOrderResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                //成功，拿到回传的结果回去处理
                OrderPaymentManager user = new OrderPaymentManager();
                user.setError(responseObj.getBody().getError());
                user.setErrorMsg(responseObj.getBody().getErrorMsg());
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }
        } catch (Exception e) {

            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }

    }

    /**
     * Demo 去北京开通流量的实现类
     *
     * @author lm
     * @date 2019/07/23
     */
    public OrderPaymentManager openPackage(String partnerCode, String orderId, String secret) {
        try {

            //请求参数处理
            OpenMonthlyPackageRequestBody body = new OpenMonthlyPackageRequestBody();
            body.setOrderId(orderId);
            // 请求参数拼接
            OpenMonthlyPackageRequest request = new OpenMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_openPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OpenMonthlyPackageRequest.class).toXml(request, "utf-8");
            logger.info("【开通流量】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_openPackage);

            logger.info("【开通流量】xml响应：" + result);
            OpenMonthlyPackageResponse responseObj = new JaxbUtil(OpenMonthlyPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                //成功，拿到回传的结果回去处理
                OrderPaymentManager user = new OrderPaymentManager();
                user.setOrderId(responseObj.getBody().getOrderId());
                user.setOrderStartDate(responseObj.getBody().getOrderStartDate());
                user.setOrderExpireDate(responseObj.getBody().getOrderExpireDate());
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    /**
     * 去北京做 3.12	续订流量套餐:2.0接口
     *
     * @author lm
     * @date 2019/3/4
     */

    public OrderPaymentManager renewPackage(String renewPeriod, String orderId, String secret, String partnerCode) {
        try {
            RenewPackagesRequestBody body = new RenewPackagesRequestBody();
            body.setOrderId(orderId);
            body.setRenewPeriod(renewPeriod);

            // 请求参数拼接
            RenewPackagesRequest request = new RenewPackagesRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_renewPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(RenewPackagesRequest.class).toXml(request, "utf-8");
            logger.info("【续订流量套餐】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_renewPackage);

            logger.info("【续订流量套餐】xml响应：" + result);

            RenewPackagesResponse responseObj = new JaxbUtil(RenewPackagesResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                // 成功:北京回传的orderId
                String orderCode = responseObj.getBody().getOrderId();
                String expireDate = responseObj.getBody().getOrderExpireDate();
                OrderPaymentManager user = new OrderPaymentManager();
                user.setOrderId(orderCode);
                user.setOrderExpireDate(expireDate);
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            }

        } catch (Exception e) {

            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }

    }
}
